package com.myk.game.gobangchess.ai.startzone;

import java.util.Arrays;
import java.util.Random;

public class CrossoverAlgorithm {
    public static void main(String[] args) {
        double crossoverRate = 0.35;
        int n = 46;
        int m = 14;
        int[][] base_board = {
            {3, 3, 3, 1, 2, 3, 3, 3, 3},
            {3, 3, 0, 0, 0, 0, 0, 0, 3},
            {3, 0, 0, 0, 0, 0, 0, 0, 3},
            {1, 0, 0, 0, 0, 0, 0, 0, 3},
            {2, 0, 0, 0, 2, 0, 0, 0, 2},
            {3, 0, 0, 0, 0, 0, 0, 0, -1},
            {3, 0, 0, 0, 0, 0, 0, 0, 3},
            {3, 0, 0, 0, 0, 0, 0, 3, 3},
            {3, 3, 3, 3, 2, -1, 3, 3, 3}
        };
        int color = 1;
        int[][] selected = {
            {0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0},
            {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
            {1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0}};
        

        int[][] children = crossover(selected, crossoverRate, n ,m, base_board, color);

        System.out.println("\nChildren Population:");
        for (int[] child : children) {
            System.out.println(Arrays.toString(child));
        }
    }

    public static int[][] crossover(int[][] selected, double crossoverRate, int n, int m, int[][] base_board, int color) {
        int[][] children = new int[selected.length][n];
        Random rand = new Random();

        for (int i = 0; i < selected.length; i++) {
            System.arraycopy(selected[i], 0, children[i], 0, n);
        }

        int[] pair = new int[selected.length];
        for (int i = 0; i < selected.length; i++) {
            pair[i] = i;
        }
        
        // 使用Fisher-Yates洗牌算法打乱pair数组
        Random rand1 = new Random(); // 创建一个随机数生成器
        for (int i = pair.length - 1; i > 0; i--) {
            // 生成一个从0到i的随机索引
            int j = rand1.nextInt(i + 1);
        
            // 交换pair[i]和pair[j]
            int temp = pair[i];
            pair[i] = pair[j];
            pair[j] = temp;
        }
        for (int i = 0; i < pair.length - 1; i += 2) {
            if (rand.nextDouble() < crossoverRate) {
                int point = rand.nextInt(n - 1) + 1;

                for (int j = 0; j < point; j++) {
                    int temp = children[pair[i]][j];
                    children[pair[i]][j] = children[pair[i + 1]][j];
                    children[pair[i + 1]][j] = temp;
                }

                // 确保交叉后1的数量不变
                children[pair[i]] = GeneFixer.fixGenes(children[pair[i]], m, base_board, color);
                children[pair[i + 1]] = GeneFixer.fixGenes(children[pair[i + 1]], m, base_board, color);
            }
        }

        return children;
    }

}
